﻿#include "dialogtip.h"
#include "ui_dialogtip.h"

#include <QDebug>

#include "MaterialDesignIcons.h"

DialogTip::DialogTip(QWidget *parent,QString strTip,int iTime) :
    QDialog(parent)
    , ui(new Ui::DialogTip())
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    setWindowFlags(
        (this->windowFlags() & ~(Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint)) |
        Qt::WindowCloseButtonHint
    );
    setWindowFlags(windowFlags() |
        Qt::BypassWindowManagerHint |
        Qt::WindowDoesNotAcceptFocus);

    setWindowTitle("提示");
    m_lbTip = ui->lbTip;
    m_pbOk = ui->pbOk;
    m_lbIcon = ui->lbIcon;

    m_lbIcon->setPixmap(QPixmap(":/showModal/resources/info.png"));
    if (iTime < 0)
        m_lbIcon->setPixmap(QPixmap(":/showModal/resources/error.png"));

    m_lbTip->setScaledContents(false);
    m_lbTip->setWordWrap(true);

    QFont font(m_lbTip->font());
    font.setPointSize(12);    
    m_lbTip->setFont(font);

    m_lbTip->setText(strTip);
    m_iTimeOut =abs(iTime);
    if(m_iTimeOut > 0)
    {
        QString str;
        str.sprintf("关闭(%d)", m_iTimeOut);
        m_pbOk->setText(str);

        //定时器
        m_timer =new QTimer(this);
        connect(m_timer,SIGNAL(timeout()),this,SLOT(onTimer()));
        m_timer->start(1000);
    }
    else
        m_pbOk->setText("关闭");


    m_pbOk->setFont(font);

    connect(m_pbOk,SIGNAL(clicked(bool)),this,SLOT(on_pbOk_clicked()));

    QString button_style = "QToolButton {color:#333333;icon-size: 20px 20px;padding-left: 5px; background-color: white; border: none; border-radius:2px;border-left: 5px solid white;}"
        "QToolButton:hover { color:#69C33B;background-color: #EDEDED;border-left: 5px solid #EDEDED; }"
        "QToolButton:checked {color:#58D68D; background-color: #EDEDED; border-left: 5px solid #58D68D; }";

    ui->pbOk->setStyleSheet(button_style);
    ui->pbOk->setIcon(QIcon(material::pixmap("folder-cancel-outline", QSize(20, 20), QColor("#333333"))));
}

DialogTip::~DialogTip()
{
    //
}

void DialogTip::onTimer()
{
    m_iTimeOut--;
    if(m_iTimeOut > 0)
    {
        QString str;
        str.sprintf("关闭(%d)", m_iTimeOut);
        m_pbOk->setText(str);
    }
    else
        this->close();
}

void DialogTip::on_pbOk_clicked()
{
    if (m_timer) m_timer->stop();
    this->close();
}
